<template>

  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <div v-if="crud.props.searchToggle">
        <!-- 搜索 -->
        <el-input
          v-model="query.name"
          clearable
          placeholder="姓名"
          style="width: 185px"
          class="filter-item"
          @keyup.enter.native="crud.toQuery"
        />
        <el-select
          v-model="query.sex"
          filterable
          clearable
          placeholder="性别"
          class="filter-item"
        >
          <el-option
            v-for="item in dict.UserGenderEnum"
            :key="item.id"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
        <date-range-picker
          v-model="query.gmtCreate"
          start-placeholder="创建日期开始"
          end-placeholder="创建日期结束"
          class="date-item"
        />
        <rrOperation :crud="crud" />
      </div>
      <!--如果想在工具栏加入更多按钮，可以使用插槽方式， slot = 'left' or 'right'-->
      <crudOperation :permission="permission" />
      <!--表单组件-->
      <el-dialog
        :close-on-click-modal="false"
        :before-close="crud.cancelCU"
        :visible.sync="crud.status.cu > 0"
        :title="crud.status.title"
        width="500px"
      >
        <el-form
          ref="form"
          v-loading="crud.editLoading"
          :model="form"
          :rules="rules"
          size="small"
          label-width="80px"
        >
          <el-form-item label="姓名" prop="name">
            <el-input v-model="form.name" style="width: 370px" />
          </el-form-item>
          <el-form-item label="性别">
            <el-select v-model="form.sex" filterable placeholder="请选择">
              <el-option
                v-for="item in dict.UserGenderEnum"
                :key="item.id"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="爱好">
            <el-select v-model="form.hobby" filterable placeholder="请选择">
              <el-option
                v-for="item in dict.hobby"
                :key="item.id"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="text" @click="crud.cancelCU">取消</el-button>
          <el-button
            :loading="crud.status.cu == 2"
            type="primary"
            @click="crud.submitCU"
          >确认</el-button>
        </div>
      </el-dialog>
      <!--表格渲染-->
      <el-table
        ref="table"
        v-loading="crud.loading"
        highlight-current-row
        stripe
        :data="crud.data"
        size="small"
        style="width: 100%"
        @selection-change="crud.selectionChangeHandler"
      >
        <el-table-column type="selection" width="55" />
        <el-table-column prop="name" label="姓名" />
        <el-table-column prop="sexName" label="性别" />
        <el-table-column prop="className" label="班级" />
        <el-table-column prop="hobbyName" label="爱好" />
        <el-table-column prop="gmtCreate" label="创建时间" />
        <el-table-column
          v-permission="['admin', 'student:edit', 'student:del']"
          label="操作"
          width="300px"
          align="center"
        >
          <template v-slot="scope">
            <div style="display: inline-block">
              <udOperation :data="scope.row" :permission="permission" />
            </div>
            <el-tooltip content="日志" effect="dark" placement="top">
              <el-button
                v-permission="['student:list']"
                size="mini"
                type="warning"
                icon="el-icon-notebook-2"
                style="display: inline-block"
                @click="handleLog(scope.row)"
              />
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>
      <!--分页组件-->
      <pagination />
    </div>
    <el-dialog title="操作日志" :visible.sync="logVisible" width="50%">
      <log ref="log" />
    </el-dialog>
  </div>
</template>

<script>
import crudStudent from '@/api/demo/student'
import CRUD, { crud, form, header, presenter } from '@crud/crud'
import rrOperation from '@crud/RR.operation'
import crudOperation from '@crud/CRUD.operation'
import udOperation from '@crud/UD.operation'
import pagination from '@crud/Pagination'
import DateRangePicker from '@/components/DateRangePicker'
import log from '@/components/Log'

const defaultForm = {
  id: null,
  name: null,
  sex: 1,
  classId: 1,
  hobby: '0'
}
export default {
  name: 'Student',
  components: {
    pagination,
    crudOperation,
    rrOperation,
    udOperation,
    DateRangePicker,
    log
  },
  mixins: [presenter(), header(), form(defaultForm), crud()],
  dicts: ['UserGenderEnum', 'hobby'],
  cruds() {
    return CRUD({
      title: '用户',
      url: 'demo/students',
      idField: 'id',
      crudMethod: { ...crudStudent }
    })
  },
  data() {
    return {
      logVisible: false,
      permission: {
        add: ['admin', 'student:add'],
        edit: ['admin', 'student:edit'],
        del: ['admin', 'student:del']
      },
      rules: {
        name: [{ required: true, message: '姓名不能为空', trigger: 'blur' }],
        sex: [{ required: true, message: '性别不能为空', trigger: 'blur' }],
        hobby: [{ required: true, message: '爱好不能为空', trigger: 'blur' }]
      },
      queryTypeOptions: [
        { key: 'name', display_name: '姓名' },
        { key: 'sex', display_name: '性别' }
      ]
    }
  },
  methods: {
    // 钩子：在获取表格数据之前执行，false 则代表不获取数据
    [CRUD.HOOK.beforeRefresh]() {
      return true
    },
    handleLog(data) {
      this.logVisible = true
      this.$nextTick(() => {
        if (this.$refs.log) {
          this.$refs.log.query.bizKey = 'students'
          this.$refs.log.query.bizNo = data.id
          this.$refs.log.crud.toQuery()
        }
      })
    }
  }
}
</script>

<style scoped></style>
